package yunquan.wang.modules.base.service.impl;

import java.util.Collection;
import java.util.Date;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;

import yunquan.wang.base.dao.DaoUtils;
import yunquan.wang.base.spring.security.SpringSecurityUtils;
import yunquan.wang.base.spring.security.UserDetail;
import yunquan.wang.modules.base.dao.AttachmentInfoDao;
import yunquan.wang.modules.base.dao.MessagesDao;
import yunquan.wang.modules.base.dao.RecevieMessageDao;
import yunquan.wang.modules.base.entity.Messages;
import yunquan.wang.modules.base.entity.RecevieMessage;
import yunquan.wang.modules.base.entity.UserBasic;
import yunquan.wang.modules.base.service.MessageService;

public class MessageServiceImpl  implements MessageService{
private	RecevieMessageDao  recevieMessageDao;
private MessagesDao  messagesDao;
private AttachmentInfoDao  attachmentInfoDao;
	@Override
	public Page<Messages> getSendMessage(Specifications spec,
			Pageable page) {
		UserDetail user=	SpringSecurityUtils.getCurrentUser();
		spec=spec.and(DaoUtils.eq("sendUser.id", user.getUserId()));
		return messagesDao.findAll(spec, page);
	}

	@Override
	//TODO
	public Page<Messages> recevieMessage(Specifications spec,
			Pageable page) {
		UserDetail user=	SpringSecurityUtils.getCurrentUser();
		spec=spec.and(DaoUtils.eq("recevieUserId", user.getUserId()));
		return messagesDao.findAll(spec, page);
	}

	@Override
	public void sendMessage(Messages entity) {
	UserDetail user=	SpringSecurityUtils.getCurrentUser();
	UserBasic userBasic=new UserBasic();
	userBasic.setId(user.getUserId());
		entity.setSendUser(userBasic);
		entity.setSendDate(new Date());
		entity.setSuccess(true);
		messagesDao.save(entity);
		for(String userId :entity.getRecevieUserIds().split("\\,"))
		{
			RecevieMessage recevieMessage=new RecevieMessage();
			UserBasic userrecevie=new UserBasic();
			userrecevie.setId(userId);
			recevieMessage.setHasRead(false);
			recevieMessage.setRecevieUser(userrecevie);
			recevieMessage.setMessages(entity);
			recevieMessageDao.save(recevieMessage);
		}
		
	}

	@Override
	public void saveMessage(Messages endity) {
		UserDetail user=	SpringSecurityUtils.getCurrentUser();
		UserBasic userBasic=new UserBasic();
		userBasic.setId(user.getUserId());
		endity.setSuccess(false);
		messagesDao.save(endity);
	}

	@Override
	public Integer queryUnreadMessage() {
		final UserDetail user=	SpringSecurityUtils.getCurrentUser();
	long count=	recevieMessageDao.count(new Specification(){
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query,
					CriteriaBuilder cb) {
				Specification spec=	 DaoUtils.eq("recevieUser.id", user.getUserId());
				return spec.toPredicate(root, query, cb);
			}});
		return new Long(count).intValue();
	}

	@Override
	public Messages readMessage(String id) {
		Messages message=	messagesDao.findOne(id);
		return message;
	}

	@Override
	public void deleteRecevieMessage(String id) {
		RecevieMessage remessage=	recevieMessageDao.findOne(id);
		Messages message=remessage.getMessages();
		recevieMessageDao.delete(remessage);
		 if(message.getSenddelete() &&message.getRecevieMessages().size()==0)
		 {
			 messagesDao.delete(message);
		 }
	}


	
	@Override
	public void deleteSendMessage(String id) {
		Messages remessage=	messagesDao.findOne(id);
 if(remessage.getRecevieMessages().size()==0)
		recevieMessageDao.delete(id);
 else
 {
	 remessage.setSenddelete(true);
	 messagesDao.save(remessage);
 }
		
	}

	@Override
	public void deleteAttachements(Collection ids) {
 
		attachmentInfoDao.delete(ids);
	}

}
